package servlet.admin.music;

import com.alibaba.fastjson.JSON;
import pojo.Result;
import pojo.Songs;
import service.MusicMngService;
import service.impl.MusicMngServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：杨晋
 */

@WebServlet("/admin/getSongByIdServlet")
public class GetSongByIdServlet extends HttpServlet {
    private MusicMngService musicMngService = new MusicMngServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户ID参数
        String userIdParam = request.getParameter("songId");
        if (userIdParam == null || userIdParam.isEmpty()) {
            response.setContentType("application/json;charset=UTF-8");
            Result result = Result.error("歌曲ID不能为空");
            response.getWriter().write(JSON.toJSONString(result));
            return;
        }

        int songId;
        try {
            songId = Integer.parseInt(userIdParam);
        } catch (NumberFormatException e) {
            response.setContentType("application/json;charset=UTF-8");
            Result result = Result.error("用户ID格式不正确");
            response.getWriter().write(JSON.toJSONString(result));
            return;
        }

        // 获取用户信息
        Songs song = musicMngService.getSongById(songId);

        response.setContentType("application/json;charset=UTF-8");

        if (song != null) {
            Result result = Result.success(song);
            response.getWriter().write(JSON.toJSONString(result));
        } else {
            Result result = Result.error("歌曲不存在");
            response.getWriter().write(JSON.toJSONString(result));
        }
    }
}